import java.util.Arrays;

public class MyStack<E> {
    private Object[] stack;
    private int size;

    public MyStack() {
        stack = new Object[10];
        size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    @SuppressWarnings("unchecked")
    public E peek() {
        if (isEmpty()) {
            return null;
        }
        return (E) stack[size - 1];
    }

    @SuppressWarnings("unchecked")
    public E pop() {
        if (isEmpty()) {
            return null;
        }
        E e = (E) stack[size - 1];
        stack[size - 1] = null;
        size--;
        return e;
    }

    public void push(E e) {
        ensureCapacity(size + 1);
        size++;
        stack[size - 1] = e;
    }

    public void ensureCapacity(int newSize) {
        int maxSize = stack.length;
        if (newSize > maxSize) {
            stack = Arrays.copyOf(stack, maxSize * 2);
            System.out.println("扩容啦");
        }
    }
}
